package com.wsc.wisdomchina.ai.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * Created by liuhaigang on 2016/12/19.
 */
public class DbManager {
    private static DbHelper db;
    private static Context context;
    private static SQLiteDatabase sqlitedatabase;

    public DbManager() {
    }

    public static void init(Context mcontext) {
        context = mcontext;
        db = getIntance();
    }

    private static DbHelper getIntance() {
        if (db == null) {
            db = new DbHelper(context);
        }
        return db;
    }

    //打开数据库连接
    public static void opendb(Context context) {
        db = new DbHelper(context);
        sqlitedatabase = db.getWritableDatabase();
    }
    //关闭数据库连接
    public static void closedb(Context context) {
        if (sqlitedatabase.isOpen()) {
            sqlitedatabase.close();
        }
    }
//    CSTableName+" (,type_user integer not null,type_message integer not null,content_text varchar(200),path varchar(50),time_voice integer,times varchar(13))");
//    IBenTableName+" (,type_user integer  not null,type_message integer not null,content_text varchar(200),path,time_voice integer,times varchar(13),richtitle varchar(50)," +
//            " richImgpath varchar(50),richDesc varchar(50) )");

    //相与客服聊天表中插入数据
//    public static void insertIntoCS(Integer typeUser, Integer typeMessage, String contextText, String path, Integer timeVoice, String times) {
//        ContentValues values = new ContentValues();
//        values.put("type_user", typeUser);
//        values.put("type_message", typeMessage);
//        values.put("content_text", contextText);
//        values.put("path", path);
//        values.put("time_voice", timeVoice);
//        values.put("times", times);
//        opendb(context);
//        sqlitedatabase.insert(DBContant.CSTableName, null, values);
//        closedb(context);
//    }

    //相与小笨聊天表中插入数据
    public static void insertIntoChatData(Integer typeUser, Integer typeMessage, String contextText, String path, Float timeVoice,
                                      String times, String richTitle, String richImgPath, String richDesc) {
        ContentValues values = new ContentValues();
        values.put("type_user", typeUser);
        values.put("type_message", typeMessage);
        values.put("content_text", contextText);
        values.put("path", path);
        values.put("time_voice", timeVoice);
        values.put("richtitle", richTitle);
        values.put("richImgpath", richImgPath);
        values.put("richDesc", richDesc);
        values.put("times", times);
        opendb(context);
        sqlitedatabase.insert(DBContant.IBenTableChatData, null, values);
        closedb(context);
    }
//    //返回 与客服聊天
//    public static Cursor selectCS(){
//        String sql=" select * from " +DBContant.CSTableName;
//        opendb(context);
//        Cursor cursor = sqlitedatabase.rawQuery(sql,null);
//        if(cursor==null){
//            return null;
//        }
//        if(cursor.getCount()>0){
//            return cursor;
//        }
//        return null;
//    }
    //返回 与客服聊天
    public static Cursor selectChatData(){
        opendb(context);
        String sql=" select * from " +DBContant.IBenTableChatData;
        if(sqlitedatabase==null){
            return null;
        }

        Cursor cursor = sqlitedatabase.rawQuery(sql,null);
        if(cursor==null){
            return null;
        }
        if(cursor.getCount()>0){
            return cursor;
        }
        return null;
    }
    //删除客服聊天表数据
    public static void deleteCS() {
        opendb(context);
        try {
            sqlitedatabase.delete(DBContant.CSTableName, null, null);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closedb(context);
        }
    }

    //删除客服聊天表数据
    public static void deleteIBen() {
        opendb(context);
        try {
            sqlitedatabase.delete(DBContant.IBenTableName, null, null);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closedb(context);
        }
    }

}
